// miniprogram/pages/homePage/usedGoods/details/details.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        mes: {
            type: '',  //消息提示类型
            showMessage: false,
            text: '',   //消息提示内容,
            icon: ''   //图标
        },
        status: '',
        message: {},
        show: false
    },
    gotoChat() {
        const that = this
        wx.navigateTo({
            url: '/pages/message/chatroom/chatroom',
            success: function (res) {
                // 通过eventChannel向被打开页面传送数据
                res.eventChannel.emit('details_chatroom', that.data.message)
            }
        })
    },
    shop(e) {//加入购物车
        wx.cloud.callFunction({
            // 云函数名称
            name: 'changeGoodsList',
            // 传给云函数的参数
            data: {
                type: "gwCar_list",
                goods: this.data.message
            },
        }).then(res => {
            // console.log(res.result) // 3
            if (res.result != "repeat") {
                this.setData({
                    mes: {
                        showMessage: true,
                        text: "成功加入购物车",
                        type: "success",
                        icon: "checked"
                    }
                })
                return setTimeout(() => {
                    this.setData({
                        mes: {
                            showMessage: false
                        }
                    })
                }, 1500)
            } else {
                this.setData({
                    mes: {
                        showMessage: true,
                        text: "已经加入购物车了",
                        type: "warn",
                        icon: "warning"
                    }
                })
                return setTimeout(() => {
                    this.setData({
                        mes: {
                            showMessage: false
                        }
                    })
                }, 1500)
            }
        }).catch(console.error)

    },
    coll(e) {  //收藏
        wx.cloud.callFunction({
            // 云函数名称
            name: 'changeGoodsList',
            // 传给云函数的参数
            data: {
                type: "collect_list",
                id: this.data.message._id
            },
        }).then(res => {
            if (res.result != "repeat") {
                this.setData({
                    mes: {
                        showMessage: true,
                        text: "成功收藏",
                        type: "success",
                        icon: "checked"
                    }
                })
                return setTimeout(() => {
                    this.setData({
                        mes: {
                            showMessage: false
                        }
                    })
                }, 1500)
            } else {
                this.setData({
                    mes: {
                        showMessage: true,
                        text: "已经收藏了",
                        type: "warn",
                        icon: "warning"
                    }
                })
                return setTimeout(() => {
                    this.setData({
                        mes: {
                            showMessage: false
                        }
                    })
                }, 1500)
            }
        }).catch(console.error)
    },
    procedure() {
        if (this.data.show) {
            this.setData({
                show: false
            })
        } else {
            this.setData({
                show: true
            })
        }

    },
    del() {
        let that = this
        wx.showModal({
            title: '提示',
            content: '确定下架',
            success(res) {
                if (res.confirm) {
                    let obj = {}
                    let dataList = []
                    obj._id = that.data.message._id
                    obj.type = that.data.message.types
                    dataList.push(obj)
                    wx.cloud.callFunction({
                        name: 'issue',
                        data: {
                            list: dataList,
                        }
                    }).then((data) => {
                        if (data.result == "ok") {
                            // that.setData({
                            //     mes: {
                            //         showMessage: true,
                            //         text: "成功下架",
                            //         type: "success",
                            //         icon: "checked"
                            //     }
                            // })
                            wx.navigateBack({
                                delta: 1
                            })
                        } else {//删除失败
                            wx.showToast({
                                title: '错误',
                                icon: 'none',
                                image: '/pages/images/icon/error.png',
                                duration: 2000
                            })
                        }
                    })
                }
            }
        })
    },
    cancel() { },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        const eventChannel = this.getOpenerEventChannel();
        // eventChannel.on('goodsList_details', (data) => {
        //     data.mes.time = data.mes.time.slice(0, 10);
        //     this.setData({
        //         message: data.mes,
        //         status: data.status
        //     })
        // })

        eventChannel.on('to_details', (data) => {
            data.mes.time = data.mes.time.slice(0, 10);
            this.setData({
                message: data.mes,
                status: data.status
            })
            this.data.message.types = "usedGoods"
        })

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {
        const cur = getCurrentPages()
        if (cur[cur.length - 2].route == "pages/homePage/usedGoods/addGoods/addGoods") {
            wx.navigateBack({
                delta: 1
            })
        }

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})